home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Pascal / Applications / NIH Image 1.60 / 1.60 Source / Init.p < prev    next >
Encoding:
Text File  |  1996-03-20  |  53.2 KB  |  2,006 lines  |  [TEXT/PJMM]

  1. unit Init;
  2.  
  3. interface
  4.  
  5.  
  6.     uses
  7.         Types, Memory, QuickDraw, QuickDrawText, Packages, Menus, Events, Fonts, 
  8.         Scrap, ToolUtils, Resources, Errors, Palettes, StandardFile, Windows,
  9.         Controls, TextEdit, Files, Dialogs, TextUtils, Finder, MixedMode, Processes,
  10.         Folders, Slots, OSUtils,
  11.         globals, Utilities, Graphics, Lut;
  12.  
  13.     procedure Init;
  14.     procedure AllocateBuffers;
  15.     procedure AllocateArrays;
  16.     procedure SetupMenus;
  17.     procedure GetPreferences(f: integer);
  18.     procedure GetSettings;
  19.     procedure FindPlugIns;
  20.     
  21.     {To compile NIH Image so that it supports Scion PCI frame grabbers you
  22.     will need to set 'SupportScionPCI' to 1 and add the following libraries
  23.     in the NIH Image project:
  24.     
  25.                     ScionPCI.lib
  26.                     PCILib
  27.                     NameRegistryLib
  28.                     DriverServicesLib
  29.     
  30.     ScionPCI.lib is included with the NIH Image source. The other libraries
  31.     are on Apple's Developer CD.}
  32.     
  33.     {$SETC SupportScionPCI := 0}
  34.  
  35.     {$ifc PowerPC and SupportScionPCI}
  36.     function LookForPCIFrameGrabbers(model: integer; var base0: longint; var base1: longint; var buffers: longint): boolean; C; EXTERNAL;
  37.     {$endc}
  38.  
  39.  
  40. implementation
  41.  
  42.  
  43.     procedure MakeCursors;
  44.         var
  45.             TempCurH: CursHandle;
  46.             i, id: integer;
  47.     begin
  48.         TempCurH := GetCursor(PickerCursorID);
  49.         if TempCurH <> nil then
  50.             begin
  51.                 PickerCursor := TempCurH^^;
  52.                 ToolCursor[PickerTool] := PickerCursor;
  53.                 ReleaseResource(handle(TempCurH));
  54.             end;
  55.  
  56.         TempCurH := GetCursor(CrossCursorPlusID);
  57.         if TempCurH <> nil then
  58.             begin
  59.                 CrossPlusCursor := TempCurH^^;
  60.                 ReleaseResource(handle(TempCurH));
  61.             end;
  62.  
  63.         TempCurH := GetCursor(CrossCursorMinusID);
  64.         if TempCurH <> nil then
  65.             begin
  66.                 CrossMinusCursor := TempCurH^^;
  67.                 ReleaseResource(handle(TempCurH));
  68.             end;
  69.  
  70.         TempCurH := GetCursor(WandPlusCursorID);
  71.         if TempCurH <> nil then
  72.             begin
  73.                 WandPlusCursor := TempCurH^^;
  74.                 ReleaseResource(handle(TempCurH));
  75.             end;
  76.  
  77.         TempCurH := GetCursor(WandMinusCursorID);
  78.         if TempCurH <> nil then
  79.             begin
  80.                 WandMinusCursor := TempCurH^^;
  81.                 ReleaseResource(handle(TempCurH));
  82.             end;
  83.  
  84.         TempCurH := GetCursor(CrossCursorID);
  85.         if TempCurH <> nil then
  86.             begin
  87.                 ToolCursor[SelectionTool] := TempCurH^^;
  88.                 ToolCursor[FreehandTool] := TempCurH^^;
  89.                 ToolCursor[PolygonTool] := TempCurH^^;
  90.                 ToolCursor[ruler] := TempCurH^^;
  91.                 ToolCursor[PlotTool] := TempCurH^^;
  92.                 ToolCursor[OvalSelectionTool] := TempCurH^^;
  93.                 ToolCursor[LineTool] := TempCurH^^;
  94.                 ToolCursor[AngleTool] := TempCurH^^;
  95.                 ToolCursor[CrossHairTool] := TempCurH^^;
  96.                 ToolCursor[LUTTool] := TempCurH^^;
  97.                 ReleaseResource(handle(TempCurH));
  98.             end;
  99.  
  100.         TempCurH := GetCursor(LUTCursorID);
  101.         if TempCurH <> nil then
  102.             begin
  103.                 LUTCursor := TempCurH^^;
  104.                 ReleaseResource(handle(TempCurH));
  105.             end;
  106.  
  107.         TempCurH := GetCursor(gmCursorID);
  108.         if TempCurH <> nil then
  109.             begin
  110.                 gmCursor := TempCurH^^;
  111.                 ReleaseResource(handle(TempCurH));
  112.             end;
  113.  
  114.         TempCurH := GetCursor(GrabberCursorID);
  115.         if TempCurH <> nil then
  116.             begin
  117.                 ToolCursor[Grabber] := TempCurH^^;
  118.                 ReleaseResource(handle(TempCurH));
  119.             end;
  120.  
  121.         TempCurH := GetCursor(PencilCursorID);
  122.         if TempCurH <> nil then
  123.             begin
  124.                 ToolCursor[Pencil] := TempCurH^^;
  125.                 ReleaseResource(handle(TempCurH));
  126.             end;
  127.  
  128.         TempCurH := GetCursor(GlassCursorPlusID);
  129.         if TempCurH <> nil then
  130.             begin
  131.                 ToolCursor[MagnifyingGlass] := TempCurH^^;
  132.                 ReleaseResource(handle(TempCurH));
  133.             end;
  134.  
  135.         TempCurH := GetCursor(GlassCursorMinusID);
  136.         if TempCurH <> nil then
  137.             begin
  138.                 GlassMinusCursor := TempCurH^^;
  139.                 ReleaseResource(handle(TempCurH));
  140.             end;
  141.  
  142.         TempCurH := GetCursor(BucketCursorID);
  143.         if TempCurH <> nil then
  144.             begin
  145.                 ToolCursor[PaintBucket] := TempCurH^^;
  146.                 ReleaseResource(handle(TempCurH));
  147.             end;
  148.  
  149.         TempCurH := GetCursor(WandCursorID);
  150.         if TempCurH <> nil then
  151.             begin
  152.                 ToolCursor[Wand] := TempCurH^^;
  153.                 ReleaseResource(handle(TempCurH));
  154.             end;
  155.  
  156.         TempCurH := GetCursor(TextCursorID);
  157.         if TempCurH <> nil then
  158.             begin
  159.                 ToolCursor[TextTool] := TempCurH^^;
  160.                 ReleaseResource(handle(TempCurH));
  161.             end;
  162.  
  163.         TempCurH := GetCursor(EraserCursorID);
  164.         if TempCurH <> nil then
  165.             begin
  166.                 ToolCursor[eraser] := TempCurH^^;
  167.                 ReleaseResource(handle(TempCurH));
  168.             end;
  169.  
  170.         TempCurH := GetCursor(SprayCursorID);
  171.         if TempCurH <> nil then
  172.             begin
  173.                 ToolCursor[SprayCanTool] := TempCurH^^;
  174.                 ReleaseResource(handle(TempCurH));
  175.             end;
  176.  
  177.         TempCurH := GetCursor(BrushCursorID);
  178.         if TempCurH <> nil then
  179.             begin
  180.                 ToolCursor[brush] := TempCurH^^;
  181.                 ReleaseResource(handle(TempCurH));
  182.             end;
  183.  
  184.         TempCurH := GetCursor(WatchCursor);
  185.         if TempCurH <> nil then
  186.             begin
  187.                 watch := TempCurH^^;
  188.                 ReleaseResource(handle(TempCurH));
  189.             end;
  190.             
  191.         for i := 1 to 8 do
  192.             AnimatedWatch[i] := watch;
  193.         id := FirstAnimatedWatchID;
  194.         for i := 2 to 8 do
  195.             begin
  196.                 TempCurH := GetCursor(id);
  197.                 id := id + 1;
  198.                 if TempCurH <> nil then
  199.                     begin
  200.                         AnimatedWatch[i] := TempCurH^^;
  201.                         ReleaseResource(handle(TempCurH));
  202.                     end;
  203.             end;
  204.  
  205.         TempCurH := GetCursor(FingerCursorID);
  206.         if TempCurH <> nil then
  207.             begin
  208.                 FingerCursor := TempCurH^^;
  209.                 ReleaseResource(handle(TempCurH));
  210.             end;
  211.             
  212.     end;
  213.  
  214.  
  215.     procedure MakeToolsBitMap;
  216.   {Creates the offscreen bitmap used to draw the tool palette.}
  217.         var
  218.             tPort: GrafPtr;
  219.             TempPort: GrafPort;
  220.             PicH: PicHandle;
  221.             FrameRect: rect;
  222.             fwidth, fheight: integer;
  223.     begin
  224.         PicH := GetPicture(130);
  225.         if PicH = nil then
  226.             ExitToShell;
  227.         FrameRect := PicH^^.PicFrame;
  228.         with FrameRect do
  229.             begin
  230.                 fwidth := right - left;
  231.                 fheight := bottom - top;
  232.             end;
  233.         GetPort(tPort);
  234.         OpenPort(@TempPort);
  235.         with ToolBits, FrameRect do
  236.             begin
  237.                 baseAddr := NewPtr((fwidth * fheight) div 8);
  238.                 if baseAddr = nil then
  239.                     ExitToShell;
  240.                 rowBytes := fwidth div 8;
  241.                 SetRect(bounds, 0, 0, fwidth, fheight);
  242.             end;
  243.         SetPortBits(ToolBits);
  244.         DrawPicture(PicH, ToolBits.bounds);
  245.         ReleaseResource(handle(PicH));
  246.         ClosePort(@TempPort);
  247.         SetPort(tPort);
  248.     end;
  249.  
  250.  
  251.     procedure InitTools;
  252.         var
  253.             ToolTop, LinesTop, i: integer;
  254.             Tool: ToolType;
  255.     begin
  256.         MakeToolsBitmap;
  257.         FirstTool := MagnifyingGlass;
  258.         LastTool := CrossHairTool;
  259.         CurrentTool := SelectionTool;
  260.         isSelectionTool := true;
  261.         PreviousTool := CurrentTool;
  262.         ToolTop := 0;
  263.         for tool := FirstTool to LastTool do
  264.             with ToolRect[tool] do
  265.                 begin
  266.                     top := ToolTop;
  267.                     bottom := top + tmiddle;
  268.                     if odd(ord(tool) + 1) then
  269.                         left := 0
  270.                     else
  271.                         begin
  272.                             left := tmiddle;
  273.                             ToolTop := ToolTop + tmiddle;
  274.                         end;
  275.                     right := left + tmiddle;
  276.                 end;
  277.         ToolTime := 0;
  278.         LutTime := 0;
  279.         StartOfLines := ToolRect[LastTool].bottom - 1;
  280.         LinesTop := StartOfLines + 10;
  281.         for i := 1 to nLineTypes do
  282.             with lines[i] do
  283.                 begin
  284.                     left := LinesLeft;
  285.                     top := LinesTop;
  286.                     right := LinesRight;
  287.                     case i of
  288.                         1, 2, 3, 4: 
  289.                             bottom := top + i;
  290.                         5: 
  291.                             bottom := top + 6;
  292.                         6: 
  293.                             bottom := top + 8
  294.                     end;
  295.                     LinesTop := bottom + 4;
  296.                 end;
  297.         LineWidth := 1;
  298.         LineIndex := 1;
  299.         with CheckRect do
  300.             begin
  301.                 left := 0;
  302.                 top := StartOfLines;
  303.                 right := LinesLeft;
  304.                 bottom := theight;
  305.             end;
  306.     end;
  307.  
  308.  
  309.     procedure AllocateBuffers;
  310.         var
  311.             tPort: GrafPtr;
  312.             err: OSErr;
  313.             BufSizeStr: str255;
  314.             atemp: integer;
  315.             SaveGDevice: GDHandle;
  316.     begin
  317.         SaveGDevice := GetGDevice;
  318.         SetGDevice(osGDevice);
  319.         GetPort(tPort);
  320.         NumToString(BufferSize div 1024, BufSizeStr);
  321.         BigBufSize := BufferSize * 2;
  322.         if FreeMem > (BigBufSize + 300000) then
  323.             BigBuf := NewPtr(BigBufSize)
  324.         else
  325.             BigBuf := nil;
  326.         if BigBuf = nil then
  327.             BigBufSize := 0;
  328.         if BigBuf <> nil then
  329.             UndoBuf := BigBuf
  330.         else
  331.             begin
  332.                 if FreeMem > (BufferSize + 200000) then
  333.                     UndoBuf := NewPtr(BufferSize)
  334.                 else
  335.                     UndoBuf := nil;
  336.             end;
  337.         if UndoBuf <> nil then
  338.             UndoBufSize := BufferSize
  339.         else
  340.             begin
  341.                 PutError(concat('There is not enough memory available to allocate the ', BufSizeStr, 'K Undo buffer. Many operations may fail or be Undoable.'));
  342.                 UndoBufSize := 0;
  343.             end;
  344.         if BigBuf <> nil then
  345.             ClipBuf := ptr(ord4(BigBuf) + BufferSize)
  346.         else
  347.             begin
  348.                 if FreeMem > (BufferSize + 300000) then
  349.                     ClipBuf := NewPtr(BufferSize)
  350.                 else
  351.                     ClipBuf := nil;
  352.             end;
  353.         UndoInfoRec := NoInfo^;
  354.         UndoInfo := @UndoInfoRec;
  355.         if UndoBuf <> nil then
  356.             with UndoInfo^ do
  357.                 begin
  358.                     roiRgn := NewRgn;
  359.                     PicBaseAddr := UndoBuf;
  360.                     osPort := CGrafPtr(NewPtr(SizeOf(CGrafPort)));
  361.                     OpenCPort(osPort);
  362.                     osPort^.portPixMap^^.BaseAddr := PicBaseAddr;
  363.                     SetPalette(WindowPtr(osPort), ExplicitPalette, false);
  364.                 end;
  365.         if ClipBuf <> nil then
  366.             begin
  367.                 ClipBufSize := BufferSize;
  368.                 ClipBufInfoRec := NoInfo^;
  369.                 ClipBufInfo := @ClipBufInfoRec;
  370.                 with ClipBufInfo^ do
  371.                     begin
  372.                         roiRgn := NewRgn;
  373.                         PicBaseAddr := ClipBuf;
  374.                         osPort := CGrafPtr(NewPtr(SizeOf(CGrafPort)));
  375.                         OpenCPort(osPort);
  376.                         osPort^.portPixMap^^.BaseAddr := PicBaseAddr;
  377.                         BytesPerRow := 0;
  378.                         SetPalette(WindowPtr(osPort), ExplicitPalette, false);
  379.                     end;
  380.             end
  381.         else
  382.             begin
  383.                 PutError(concat('There is not enough memory available to allocate the ', BufSizeStr, 'K Clipboard Buffer. Many operations, including Copy and Paste, may fail.'));
  384.                 ClipBufSize := 0;
  385.             end;
  386.         SetPort(tPort);
  387.         SetGDevice(SaveGDevice);
  388.     end;
  389.  
  390.  
  391.     procedure AllocateArrays;
  392.         var
  393.             nItems: LongInt;
  394.             i: integer;
  395.  
  396.         procedure Abort;
  397.         begin
  398.             PutError('Not enough memory available to allocate arrays.');
  399.             ExitToShell;
  400.         end;
  401.  
  402.     begin
  403.         PlotData := rLinePtr(NewPtr(SizeOf(rLineType)));
  404.         if PlotData = nil then
  405.             abort;
  406.         nItems := maxCoordinates;
  407.         xCoordinates := xCoordinatesPtr(NewPtr(nItems * SizeOf(integer)));
  408.         if xCoordinates = nil then
  409.             abort;
  410.         yCoordinates := yCoordinatesPtr(NewPtr(nItems * SizeOf(integer)));
  411.         if yCoordinates = nil then
  412.             abort;
  413.         nItems := MaxMeasurements + 1;
  414.         mean := meanPtr(NewPtr(nItems * SizeOf(real)));
  415.         if mean = nil then
  416.             abort;
  417.         sd := sdPtr(NewPtr(nItems * SizeOf(real)));
  418.         if sd = nil then
  419.             abort;
  420.         PixelCount := PixelCountPtr(NewPtr(nItems * SizeOf(LongInt)));
  421.         if PixelCount = nil then
  422.             abort;
  423.         mArea := AreaPtr(NewPtr(nItems * SizeOf(real)));
  424.         if mArea = nil then
  425.             abort;
  426.         mode := modePtr(NewPtr(nItems * SizeOf(real)));
  427.         if mode = nil then
  428.             abort;
  429.         IntegratedDensity := IntegratedDensityPtr(NewPtr(nItems * SizeOf(real)));
  430.         if IntegratedDensity = nil then
  431.             abort;
  432.         idBackground := idBackgroundPtr(NewPtr(nItems * SizeOf(real)));
  433.         if idBackground = nil then
  434.             abort;
  435.         xcenter := xcenterPtr(NewPtr(nItems * SizeOf(real)));
  436.         if xcenter = nil then
  437.             abort;
  438.         ycenter := ycenterPtr(NewPtr(nItems * SizeOf(real)));
  439.         if ycenter = nil then
  440.             abort;
  441.         MajorAxis := MajorAxisPtr(NewPtr(nItems * SizeOf(real)));
  442.         if MajorAxis = nil then
  443.             abort;
  444.         MinorAxis := MinorAxisPtr(NewPtr(nItems * SizeOf(real)));
  445.         if MinorAxis = nil then
  446.             abort;
  447.         orientation := orientationPtr(NewPtr(nItems * SizeOf(real)));
  448.         if orientation = nil then
  449.             abort;
  450.         mMin := MinPtr(NewPtr(nItems * SizeOf(real)));
  451.         if mMin = nil then
  452.             abort;
  453.         mMax := MaxPtr(NewPtr(nItems * SizeOf(real)));
  454.         if mMax = nil then
  455.             abort;
  456.         plength := plengthPtr(NewPtr(nItems * SizeOf(real)));
  457.         if plength = nil then
  458.             abort;
  459.         User1 := User1Ptr(NewPtr(nItems * SizeOf(real)));
  460.         if User1 = nil then
  461.             abort;
  462.         for i := 0 to MaxMeasurements do
  463.             User1^[i] := 0.0;
  464.         User2 := User2Ptr(NewPtr(nItems * SizeOf(real)));
  465.         if User2 = nil then
  466.             abort;
  467.         for i := 0 to MaxMeasurements do
  468.             User2^[i] := 0.0;
  469.         ClearResults(0);
  470.         if FreeMem < MinFree then
  471.             Abort;
  472.     end;
  473.  
  474.  
  475.     procedure GetKernelsWorkingDir (var settings: SettingsType);
  476.         var
  477.             wdRefNum: integer;
  478.             err: OSErr;
  479.     begin
  480.         with settings do
  481.             if sKernelsVRefNum <> 0 then
  482.                 begin
  483.                     err := OpenWD(sKernelsVRefNum, sKernelsDirID, 0, wdRefNum);
  484.                     if err = NoErr then
  485.                         KernelsRefNum := wdRefNum;
  486.           {ShowMessage(concat('KernelsRefNum=', long2str(KernelsRefNum), crStr, 'vRefNum=', long2str(sKernelsVRefNum), crStr, 'DirID=', long2str(sKernelsDirID)));}
  487.                 end;
  488.     end;
  489.  
  490.  
  491.     procedure GetDefaultWorkingDir (var settings: SettingsType);
  492.         var
  493.             wdRefNum: integer;
  494.             err: OSErr;
  495.     begin
  496.         with settings do
  497.             if sDefaultVRefNum <> 0 then
  498.                 begin
  499.                     err := OpenWD(sDefaultVRefNum, sDefaultDirID, 0, wdRefNum);
  500.                     if err = NoErr then
  501.                         DefaultRefNum := wdRefNum;
  502.                 end;
  503.     end;
  504.  
  505.  
  506.  
  507. procedure GetPreferences(f: integer);
  508.         var
  509.             err: OSErr;
  510.             ByteCount: LongInt;
  511.             ok: boolean;
  512.             size: LongInt;
  513.             settings: SettingsType;
  514.             i: integer;
  515.             SaveInfo: InfoPtr;
  516.             okay: boolean;
  517.     begin
  518.         err := GetEof(f, ByteCount);
  519.         if ByteCount > SizeOf(settings) then
  520.             ByteCount := SizeOf(settings);
  521.         err := fsRead(f, ByteCount, @settings);
  522.         if err <> NoErr then
  523.             exit(GetPreferences);
  524.         err := fsClose(f);
  525.         with settings, info^ do
  526.             begin
  527.                 if sID <> 'IMAG' then begin
  528.                         PutError('The "Image Prefs" file in the System folder is corrupted. Please delete it and try again.');
  529.                         exitToShell;
  530.                     end;
  531.                 ForegroundIndex := sForegroundIndex;
  532.                 BackgroundIndex := sBackgroundIndex;
  533.                 BrushHeight := sBrushHeight;
  534.                 BrushWidth := sBrushWidth;
  535.                 SprayCanDiameter := sSprayCanDiameter;
  536.                 SprayCanRadius := SprayCanDiameter div 2;
  537.                 SprayCanRadius2 := SprayCanRadius * SprayCanRadius;
  538.                 CurrentFontID := sCurrentFontID;
  539.                 CurrentStyle := SCurrentStyle;
  540.                 CurrentSize := sCurrentSize;
  541.                 TextJust := sTextJust;
  542.                 TextBack := sTextBack;
  543.                 nExtraColors := sNExtraColors;
  544.                 ExtraColors := sExtraColors;
  545.                 InvertVideo := sInvertVideo;
  546.                 Measurements := sMeasurements;
  547.                 InvertPlots := sInvertPlots;
  548.                 AutoScalePlots := sAutoScalePlots;
  549.                 LinePlot := sLinePlot;
  550.                 DrawPlotLabels := sDrawPlotLabels;
  551.                 FixedSizePlot := sFixedSizePlot;
  552.                 ProfilePlotWidth := sProfilePlotWidth;
  553.                 ProfilePlotHeight := sProfilePlotHeight;
  554.                 FramesToAverage := sFramesToAverage;
  555.                 NewPicWidth := sNewPicWidth;
  556.                 NewPicHeight := sNewPicHeight;
  557.                 BufferSize := sBufferSize;
  558.                 ThresholdToForeground := sThresholdToForeground;
  559.                 NonThresholdToBackground := sNonThresholdToBackground;
  560.                 VideoChannel := sVideoChannel;
  561.                 WhatToImport := sWhatToImport;
  562.                 ImportCustomWidth := sImportCustomWidth;
  563.                 ImportCustomHeight := sImportCustomHeight;
  564.                 ImportCustomOffset := sImportCustomOffset;
  565.         {WandAutoMeasure := sWandAutoMeasure;}
  566.                 WandAdjustAreas := sWandAdjustAreas;
  567.                 BinaryIterations := sBinaryIterations;
  568.                 ScaleArithmetic := sScaleArithmetic;
  569.                 InvertPixelValues := sInvertPixelValues;
  570.                 InvertYCoordinates := sInvertYCoordinates;
  571.                 FieldWidth := sFieldWidth;
  572.                 precision := sPrecision;
  573.                 MinParticleSize := sMinParticleSize;
  574.                 MaxParticleSize := sMaxParticleSize;
  575.                 IgnoreParticlesTouchingEdge := sIgnoreParticlesTouchingEdge;
  576.                 LabelParticles := sLabelParticles;
  577.                 OutlineParticles := sOutlineParticles;
  578.          {IncludeHoles := sIncludeHoles;}
  579.                 OscillatingMovies := sOscillatingMovies;
  580.          {DriverHalftoning := sDriverHalftoning;}
  581.                 MaxMeasurements := sMaxMeasurements;
  582.                 ImportCustomDepth := sImportCustomDepth;
  583.                 ImportSwapBytes := sImportSwapBytes;
  584.                 ImportCalibrate := sImportCalibrate;
  585.                 ImportAutoscale := sImportAutoscale;
  586.                 ShowHeadings := sShowHeadings;
  587.                 if sVersion >= 140 then
  588.                     begin
  589.                         ProfilePlotMin := sProfilePlotMin;
  590.                         ProfilePlotMax := sProfilePlotMax;
  591.                         ImportMin := sImportMin;
  592.                         ImportMax := sImportMax;
  593.                         HighlightSaturatedPixels := sHighlightPixels;
  594.                     end;
  595.                 if sVersion >= 141 then
  596.                     begin
  597.                         BallRadius := sBallRadius;
  598.                         FasterBackgroundSubtraction := sFasterBackgroundSubtraction;
  599.                         ScaleConvolutions := sScaleConvolutions;
  600.                     end;
  601.                 if sVersion >= 142 then
  602.                     begin
  603.                         BinaryCount := sBinaryCount;
  604.                         BinaryThreshold := BinaryCount * 255;
  605.                         if (not GrayMapReady) and (sLUTMode = Pseudocolor) and (sColorTable <> CustomTable) and (sColorTable <= Spectrum) then
  606.                             begin
  607.                                 ColorTable := sColorTable;
  608.                                 ColorStart := sColorStart;
  609.                                 ColorEnd := sColorEnd;
  610.                                 LutMode := Pseudocolor;
  611.                                 InvertedColorTable := sInvertedTable;
  612.                             end;
  613.                     end;
  614.                 if sVersion >= 144 then
  615.                     begin
  616.                         HalftoneFrequency := sHalftoneFrequency;
  617.                         HalftoneAngle := sHalftoneAngle;
  618.                         HalftoneDotFunction := sHalftoneDotFunction;
  619.                     end;
  620.                 if sVersion >= 146 then
  621.                     begin
  622.                         DacLow := sDacLow;
  623.                         DacHigh := sDacHigh;
  624.                     end;
  625.                 if (sVersion >= 147) and ((FrameGrabber = ScionLG3) or (FrameGrabber = ScionAG5) or (FrameGrabber = ScionVG5f)) then
  626.                     SyncMode := sSyncMode;
  627.                 if sVersion >= 149 then
  628.                     begin
  629.                         SwitchLUTOnSuspend := sSwitchLUTOnSuspend;
  630.                         VideoRateAveraging := sVideoRateAveraging;
  631.                     end;
  632.                 if sVersion >= 150 then
  633.                     begin
  634.                         ImportInvert := sImportInvert;
  635.                     end;
  636.                 if sVersion >= 152 then
  637.                     begin
  638.                         if sTextCreator[1] <> chr(0) then
  639.                             TextCreator := sTextCreator;
  640.                     end;
  641.                 if sVersion >= 156 then
  642.                     begin
  643.                         if sMathSubGain <> 0.0 then begin
  644.                             MathSubGain:=sMathSubGain;
  645.                             MathSubOffset:=sMathSubOffset;
  646.                             MathGain:=MathSubGain;
  647.                             MathOffset:=MathSubOffset;
  648.                         end;
  649.                     end;
  650.                 if sVersion >= 160 then begin
  651.                         fgScale := sfgScale;
  652.                         UseBuiltinDigitizer := sUseBuiltinDigitizer;
  653.                         DigitizerMode := sDigitizerMode;
  654.                         DigitizerStandard := sDigitizerStandard;
  655.                         LutFriendlyMode := sLutFriendlyMode;
  656.                     end;
  657.                 {GetKernelsWorkingDir(settings);}
  658.                 {GetDefaultWorkingDir(settings);}
  659.                 UpdateFitEllipse;
  660.             end; {with}
  661.     end;
  662.     
  663.     
  664.     procedure GetSettings;
  665.         var
  666.             err: OSErr;
  667.             f: integer;
  668.             ByteCount: LongInt;
  669.             ok: boolean;
  670.             size: LongInt;
  671.             settings: SettingsType;
  672.             i: integer;
  673.             PrefsFound:boolean;
  674.             PrefsVRef: integer;
  675.             PrefsDirID: LongInt;
  676.             PrefsSpec: FSSpec;
  677.     begin
  678.         PrefsFound:=false;
  679.         if System7 then begin
  680.             {Look in the Preferences folder}
  681.             err:=FindFolder(kOnSystemDisk, kPreferencesFolderType,
  682.                         kDontCreateFolder, PrefsVRef, PrefsDirID);
  683.             if err=noErr then
  684.                 err:=FSMakeFSSpec(PrefsVRef, PrefsDirID, PrefsName, PrefsSpec);
  685.             if err=noErr then
  686.                 err:=FSpOpenDF(PrefsSpec, fsCurPerm, f);
  687.             if err=noErr then
  688.                 PrefsFound:=true;
  689.         end;
  690.         if not PrefsFound then begin
  691.             {Look in the System folder}
  692.             err := fsopen(PrefsName, SystemRefNum, f);
  693.             if err <> NoErr then
  694.                 exit(GetSettings);
  695.         end;
  696.         GetPreferences(f);
  697.     end;
  698.  
  699.  
  700.     procedure MakePatterns;
  701. {Creates the patterns used to create the "marching ants". Thanks to}
  702. {Seth Snyder on CompuServe for the example.}
  703.         var
  704.             i, j: Integer;
  705.     begin
  706.         j := 0;
  707.         for i := 0 to 7 do
  708.             begin
  709.                 AntPattern[i].pat[(j + 0) mod 8] := $1F;
  710.                 AntPattern[i].pat[(j + 1) mod 8] := $3E;
  711.                 AntPattern[i].pat[(j + 2) mod 8] := $7C;
  712.                 AntPattern[i].pat[(j + 3) mod 8] := $F8;
  713.                 AntPattern[i].pat[(j + 4) mod 8] := $F1;
  714.                 AntPattern[i].pat[(j + 5) mod 8] := $E3;
  715.                 AntPattern[i].pat[(j + 6) mod 8] := $C7;
  716.                 AntPattern[i].pat[(j + 7) mod 8] := $8F;
  717.                 j := j + 1;
  718.             end;
  719.         PatIndex := 0;
  720.     end;
  721.  
  722.  
  723.     procedure InitExtraColors;
  724.         var
  725.             i, j, ctop, cbottom, entry: integer;
  726.             tRect: rect;
  727.     begin
  728.         with ExtraColors[1] do
  729.             begin
  730.                 red := -1;
  731.                 green := 0;
  732.                 blue := 0;
  733.             end;
  734.         with ExtraColors[2] do
  735.             begin
  736.                 red := 0;
  737.                 green := -1;
  738.                 blue := 0;
  739.             end;
  740.         with ExtraColors[3] do
  741.             begin
  742.                 red := 0;
  743.                 green := 0;
  744.                 blue := -1;
  745.             end;
  746.         with ExtraColors[4] do
  747.             begin
  748.                 red := -1;
  749.                 green := -1;
  750.                 blue := 0;
  751.             end;
  752.         with ExtraColors[5] do
  753.             begin
  754.                 red := 0;
  755.                 green := -1;
  756.                 blue := -1;
  757.             end;
  758.         with ExtraColors[6] do
  759.             begin
  760.                 red := -1;
  761.                 green := 0;
  762.                 blue := -1;
  763.             end;
  764.         ctop := 256;
  765.         cbottom := ctop + ExtraColorsHeight;
  766.         for i := 1 to MaxExtraPlus2 do
  767.             begin
  768.                 SetRect(tRect, 0, ctop, cwidth, cbottom);
  769.                 ExtraColorsRect[i] := tRect;
  770.                 ctop := ctop + ExtraColorsHeight;
  771.                 cbottom := cbottom + ExtraColorsHeight;
  772.             end;
  773.         ExtraColorsEntry[1] := WhiteIndex;
  774.         ExtraColorsEntry[2] := BlackIndex;
  775.         entry := FirstExtraColorsEntry;
  776.         j := 3;
  777.         for i := 1 to MaxExtraColors do
  778.             begin
  779.                 ExtraColorsEntry[j] := entry;
  780.                 j := j + 1;
  781.                 Entry := Entry + 1;
  782.             end;
  783.     end;
  784.  
  785.  
  786.     function GetSlotBase (id: integer): LongInt;
  787.   {Returns the slot base address of the NuBus card with the specified id. The address}
  788.   {returned is in the form $Fss00000, which is valid in both 24 and 32-bit modes.}
  789.   {Returns 0 if a card with the given id is not found.}
  790.         type
  791.             SPRAMRecord = packed record
  792.                     BoardId: integer;
  793.                     VenderUse: packed array[1..6] of SignedByte;
  794.                 end;
  795.         var
  796.             SlotBlock: SpBlock;
  797.             sparm: SPRAMRecord;
  798.             SparmAddr: LongInt;
  799.             i: integer;
  800.             err: OSErr;
  801.     begin
  802.         with SlotBlock do
  803.             begin
  804.                 SparmAddr := LongInt(@sparm);
  805.                 spResult := SparmAddr;
  806.                 for i := 9 to 15 do
  807.                     begin
  808.                         spSlot := i;
  809.                         err := sReadPRAMRec(@SlotBlock);
  810.                         if sparm.BoardID = id then
  811.                             begin
  812.                                 GetSlotBase := bor($F0000000, spSlot * $100000 + spSlot * $1000000);
  813.                 superSlotBase := spSlot * $10000000;
  814.                                 exit(GetSlotBase)
  815.                             end;
  816.                     end;
  817.                 GetSlotBase := 0;
  818.             end;
  819.     end;
  820.  
  821.  
  822.     procedure SetupFGPort;
  823. {So we can use CopyBits, this routine sets up a color graf port that}
  824. {uses the memory on the frame grabber board as the PixMap.}
  825.         const
  826.             baseAddr32 = 4;
  827.         var
  828.             tPort: GrafPtr;
  829.             trect: rect;
  830.             SaveGDevice: GDHandle;
  831.     begin
  832.         SaveGDevice := GetGDevice;
  833.         SetGDevice(osGDevice);
  834.         GetPort(tPort);
  835.         fgPort := CGrafPtr(NewPtr(SizeOf(CGrafPort)));
  836.         OpenCPort(fgPort);
  837.         SetRect(trect, 0, 0, fgWidth, fgHeight);
  838.         with fgPort^ do
  839.             begin
  840.                 with PortPixMap^^ do
  841.                     begin
  842.                         if PCIFrameGrabber then
  843.                             BaseAddr := ptr(fgSuperSlotBase0)
  844.                         else
  845.                             BaseAddr := ptr(fgSlotBase);
  846.                         bounds := trect;
  847.                         RowBytes := BitOr(fgRowBytes, $8000);
  848.                         pmVersion := baseAddr32; {Needed for 8*24 GC card. See TN 275.}
  849.                     end;
  850.                 PortRect := trect;
  851.                 RectRgn(visRgn, trect);
  852.             end;
  853.         hlock(handle(fgPort^.PortPixMap));
  854.         fgPixMap := fgPort^.PortPixMap;
  855.         SetPort(tPort);
  856.         SetPalette(WindowPtr(fgPort), ExplicitPalette, false);
  857.         SetGDevice(SaveGDevice);
  858.     end;
  859.  
  860.  
  861.     function is50HzLG3: boolean;
  862.         const
  863.             BoardResourceID = 1;
  864.             VendorInfoID = 36;
  865.             PartNumberID = 4;
  866.             PartNumber50Hz = 'LG-3 50Hz';
  867.         var
  868.             SlotBlock: SpBlock;
  869.             err: OSErr;
  870.             PartNumberPtr: StringPtr;
  871.     begin
  872.         is50HzLG3 := false;
  873.         SlotBlock.spSlot := byte(bsr(band(fgSlotBase, $0f000000), 24));
  874.         SlotBlock.spId := BoardResourceID;
  875.         SlotBlock.spExtDev := 0;
  876.         err := SRsrcInfo(@SlotBlock);
  877.         if err <> 0 then
  878.             exit(is50HzLG3);
  879.         SlotBlock.spId := VendorInfoID;
  880.         err := SFindStruct(@SlotBlock);
  881.         if err <> 0 then
  882.             exit(is50HzLG3);
  883.         SlotBlock.spID := PartNumberID;
  884.         err := SGetCString(@SlotBlock);
  885.         if err <> 0 then
  886.             exit(is50HzLG3);
  887.         PartNumberPtr := C2PStr(SlotBlock.spResult);
  888.         if PartNumberPtr^ = PartNumber50Hz then
  889.             is50HzLG3 := true;
  890.         DisposePtr(Ptr(SlotBlock.spResult)); {need to free memory allocated by SGetCString()}
  891.     end;
  892.  
  893.  
  894.     function is50HzAG5: boolean;
  895.         const
  896.             BoardResourceID = 1;
  897.             VendorInfoID = 36;
  898.             PartNumberID = 4;
  899.             PartNumber50Hz = 'AG-5 50Hz';
  900.         var
  901.             SlotBlock: SpBlock;
  902.             err: OSErr;
  903.             PartNumberPtr: StringPtr;
  904.     begin
  905.         is50HzAG5 := false;
  906.         SlotBlock.spSlot := byte(bsr(band(fgSlotBase, $0f000000), 24));
  907.         SlotBlock.spId := BoardResourceID;
  908.         SlotBlock.spExtDev := 0;
  909.         err := SRsrcInfo(@SlotBlock);
  910.         if err <> 0 then
  911.             exit(is50HzAG5);
  912.         SlotBlock.spId := VendorInfoID;
  913.         err := SFindStruct(@SlotBlock);
  914.         if err <> 0 then
  915.             exit(is50HzAG5);
  916.         SlotBlock.spID := PartNumberID;
  917.         err := SGetCString(@SlotBlock);
  918.         if err <> 0 then
  919.             exit(is50HzAG5);
  920.         PartNumberPtr := C2PStr(SlotBlock.spResult);
  921.         if PartNumberPtr^ = PartNumber50Hz then
  922.             is50HzAG5 := true;
  923.         DisposePtr(Ptr(SlotBlock.spResult));  {need to free memory allocated by SGetCString()}
  924.     end;
  925.  
  926.  
  927.     function is50HzVG5: boolean;
  928.         const
  929.             BoardResourceID = 1;
  930.             VendorInfoID = 36;
  931.             PartNumberID = 4;
  932.             PartNumber50Hz = 'VG-5 50Hz';
  933.         var
  934.             SlotBlock: SpBlock;
  935.             err: OSErr;
  936.             PartNumberPtr: StringPtr;
  937.     begin
  938.         is50HzVG5 := false;
  939.         SlotBlock.spSlot := byte(bsr(band(fgSlotBase, $0f000000), 24));
  940.         SlotBlock.spId := BoardResourceID;
  941.         SlotBlock.spExtDev := 0;
  942.         err := SRsrcInfo(@SlotBlock);
  943.         if err <> 0 then
  944.             exit(is50HzVG5);
  945.         SlotBlock.spId := VendorInfoID;
  946.         err := SFindStruct(@SlotBlock);
  947.         if err <> 0 then
  948.             exit(is50HzVG5);
  949.         SlotBlock.spID := PartNumberID;
  950.         err := SGetCString(@SlotBlock);
  951.         if err <> 0 then
  952.             exit(is50HzVG5);
  953.         PartNumberPtr := C2PStr(SlotBlock.spResult);
  954.         if PartNumberPtr^ = PartNumber50Hz then
  955.             is50HzVG5 := true;
  956.         DisposePtr(Ptr(SlotBlock.spResult)); {need to free memory allocated by SGetCString()}
  957.     end;
  958.  
  959.  
  960.  procedure CheckLG3Buffer;
  961.   type
  962.    LongintPtr = ^Longint;
  963.   const
  964.    Pattern = $A596A596;
  965.   var
  966.    BufferPtr: LongintPtr;
  967.    mode: signedbyte;
  968.    
  969.    procedure SwapMode;
  970.    begin
  971.       {$ifc not PowerPC}
  972.       SwapMMUMode(mode);
  973.       {$endc}
  974.    end;
  975.    
  976.  begin
  977.   mode := true32b;
  978.   SwapMode;
  979.   BufferPtr := LongintPtr(fgSlotBase);
  980.   BufferPtr^ := Pattern;
  981.   BufferPtr := LongintPtr(bsl(band(fgSlotBase, $0f000000), 4) + (524288 * 2));
  982.   if BufferPtr^ = Pattern then begin
  983.     MaxLG3Frames := 2;
  984.       SwapMode;
  985.     exit(CheckLG3Buffer);
  986.    end;
  987.   BufferPtr := LongintPtr(bsl(band(fgSlotBase, $0f000000), 4) + (524288 * 8));
  988.   if BufferPtr^ = Pattern then begin
  989.     MaxLG3Frames := 8;
  990.       SwapMode;
  991.     exit(CheckLG3Buffer);
  992.    end;
  993.   BufferPtr := LongintPtr(bsl(band(fgSlotBase, $0f000000), 4) + (524288 * 32));
  994.   if BufferPtr^ = Pattern then begin
  995.     MaxLG3Frames := 32;
  996.       SwapMode;
  997.     exit(CheckLG3Buffer);
  998.    end
  999.   else begin
  1000.     MaxLG3Frames := 128;
  1001.       SwapMode;
  1002.    end;
  1003.  end;
  1004.  
  1005.  
  1006.     procedure LookForFrameGrabbers;
  1007.         const
  1008.             ControlRegOffset = $80000;
  1009.             ChannelRegOffset = $80004;
  1010.             LG3ControlRegOffset = $C0000;
  1011.             LG3ChannelRegOffset = $C0008;
  1012.             AG5ControlRegOffset = $C0000;
  1013.             AG5ChannelRegOffset = $C0008;
  1014.             VG5ControlRegOffset = $C0000;
  1015.             VG5ChannelRegOffset = $C0008;
  1016.             VG5BufferRegOffset = $C0014;
  1017.             BufferRegOffset = $C0004;
  1018.             DacHighRegOffset = $C000C;
  1019.             DacLowRegOffset = $C0010;
  1020.             DacARegOffset = $C0014;
  1021.             DacBRegOffset = $C0018;
  1022.             ConstantRegOffset = $C0004;
  1023.             ScaleHighRegOffset = $D0000;
  1024.             ScaleLowRegOffset = $D0004;
  1025.             DT2255id = $11A;
  1026.             ScionLG3id = $48B;
  1027.             ScionAG5id = $620;
  1028.             ScionVG5id = $67E;
  1029.             ScionLG3PCIid = 1;
  1030.             ScionVG5PCIid = 2;
  1031.             PalBufferSize = 393216; {768 x 512}
  1032.         var
  1033.             err: OSErr;
  1034.             tPort: GrafPtr;
  1035.             OptionKeyIsDown: boolean;
  1036.             found : boolean;
  1037.             model: integer;
  1038.             base0, base1, buffers: longint;
  1039.     begin
  1040.         FrameGrabber := NoFrameGrabber;
  1041.         PCIFrameGrabber := false;
  1042.         DoubleBuffering := false;
  1043.         fgPixMap := nil;
  1044.         DacLow := DefaultDacLow;
  1045.         DacHigh := DefaultDacHigh;
  1046.         if ShiftKeyDown then
  1047.             exit(LookForFrameGrabbers);
  1048.         OptionKeyIsDown := OptionKeyDown;
  1049.         fgPort := nil;
  1050.         
  1051.         {Scion LG-3}
  1052.         fgSlotBase := GetSlotBase(ScionLG3id);
  1053.         fgRowBytes := 1024;
  1054.         if (fgSlotBase <> 0) and not OptionKeyIsDown then begin
  1055.                 FrameGrabber := ScionLG3;
  1056.                 ControlReg := ptr(fgSlotBase + LG3ControlRegOffset);
  1057.                 ChannelReg := ptr(fgSlotBase + LG3ChannelRegOffset);
  1058.                 BufferReg := ptr(fgSlotBase + BufferRegOffset);
  1059.                 DacHighReg := ptr(fgSlotBase + DacHighRegOffset);
  1060.                 DacLowReg := ptr(fgSlotBase + DacLowRegOffset);
  1061.                 DacAReg := ptr(fgSlotBase + DacARegOffset);
  1062.                 DacBReg := ptr(fgSlotBase + DacBRegOffset);
  1063.                 fgWidth := 640;
  1064.                 fgHeight := 480;
  1065.                 if is50HzLG3 then begin
  1066.                         fgWidth := 768;
  1067.                         fgHeight := 512;
  1068.                         if BufferSize < PalBufferSize then
  1069.                             BufferSize := PalBufferSize;
  1070.                     end;
  1071.                 CheckLG3Buffer;
  1072.                 SetupFGPort;
  1073.                 ResetScionLG3;
  1074.                 exit(LookForFrameGrabbers);
  1075.             end;
  1076.             
  1077.         {Scion AG-5}
  1078.         fgSlotBase := GetSlotBase(ScionAG5id);
  1079.         if (fgSlotBase <> 0) and not OptionKeyIsDown then begin
  1080.                 FrameGrabber := ScionAG5;
  1081.                 ControlReg := ptr(fgSlotBase + AG5ControlRegOffset);
  1082.                 ChannelReg := ptr(fgSlotBase + AG5ChannelRegOffset);
  1083.                 ConstantReg := ptr(fgSlotBase + ConstantRegOffset);
  1084.                 DacHighReg := ptr(fgSlotBase + DacHighRegOffset);
  1085.                 DacLowReg := ptr(fgSlotBase + DacLowRegOffset);
  1086.                 ScaleHighReg := IntegerPtr(fgSlotBase + ScaleHighRegOffset);
  1087.                 ScaleLowReg := IntegerPtr(fgSlotBase + ScaleLowRegOffset);
  1088.                 fgWidth := 640;
  1089.                 fgHeight := 480;
  1090.                 if is50HzAG5 then begin
  1091.                         fgWidth := 768;
  1092.                         fgHeight := 512;
  1093.                         if BufferSize < PalBufferSize then
  1094.                             BufferSize := PalBufferSize;
  1095.                     end;
  1096.                 SetupFGPort;
  1097.                 ResetScionAG5;
  1098.                 exit(LookForFrameGrabbers);
  1099.             end;
  1100.             
  1101.         {Scion VG-5}
  1102.         fgSlotBase := GetSlotBase(ScionVG5id);
  1103.         if (fgSlotBase <> 0) and not OptionKeyIsDown then begin
  1104.                 FrameGrabber := ScionVG5f;
  1105.                 ControlReg := ptr(fgSlotBase + VG5ControlRegOffset);
  1106.                 ChannelReg := ptr(fgSlotBase + VG5ChannelRegOffset);
  1107.                 DacHighReg := ptr(fgSlotBase + DacHighRegOffset);
  1108.                 DacLowReg := ptr(fgSlotBase + DacLowRegOffset);
  1109.                 fgWidth := 640;
  1110.                 fgHeight := 480;
  1111.                 if is50HzVG5 then begin
  1112.                         fgWidth := 768;
  1113.                         fgHeight := 512;
  1114.                         if BufferSize < PalBufferSize then
  1115.                             BufferSize := PalBufferSize;
  1116.                     end;
  1117.                 SetupFGPort;
  1118.                 ResetScionVG5f;
  1119.                 exit(LookForFrameGrabbers);
  1120.             end;
  1121.             
  1122.         {Data Translation QuickCapture}
  1123.         fgSlotBase := GetSlotBase(DT2255id);
  1124.         if fgSlotBase <> 0 then begin
  1125.                 FrameGrabber := QuickCapture;
  1126.                 ControlReg := ptr(fgSlotBase + ControlRegOffset);
  1127.                 ChannelReg := ptr(fgSlotBase + ControlRegOffset + 4);
  1128.                 fgWidth := 640;
  1129.                 fgHeight := 480;
  1130.                 if band(ChannelReg^, 8) = 8 then begin {Check for 50Hz(PAL) card}
  1131.                         fgWidth := 768;
  1132.                         fgHeight := 512;
  1133.                         if BufferSize < PalBufferSize then
  1134.                             BufferSize := PalBufferSize;
  1135.                     end;
  1136.                 SetupFGPort;
  1137.                 ResetQuickCapture;
  1138.                 exit(LookForFrameGrabbers);
  1139.             end;
  1140.             
  1141.             {$ifc PowerPC and SupportScionPCI}
  1142.             
  1143.             {Scion LG-3 (PCI)}
  1144.             model := ScionLG3PCIid;
  1145.             base0 := 0;
  1146.             base1 := 0;
  1147.             buffers:= 0;
  1148.             found := false;
  1149.             found := LookForPCIFrameGrabbers(model, base0, base1, buffers);
  1150.             if found then begin
  1151.                 fgSlotBase := base0;
  1152.                 fgSuperSlotBase0 := base1;
  1153.                 fgSuperSlotBase1 := base1 + 524288;
  1154.                 FrameGrabber := ScionLG3;
  1155.                 PCIFrameGrabber := true;
  1156.                 ControlReg := ptr(fgSlotBase + LG3ControlRegOffset);
  1157.                 ChannelReg := ptr(fgSlotBase + LG3ChannelRegOffset);
  1158.                 BufferReg := ptr(fgSlotBase + BufferRegOffset);
  1159.                 DacHighReg := ptr(fgSlotBase + DacHighRegOffset);
  1160.                 DacLowReg := ptr(fgSlotBase + DacLowRegOffset);
  1161.                 DacAReg := ptr(fgSlotBase + DacARegOffset);
  1162.                 DacBReg := ptr(fgSlotBase + DacBRegOffset);
  1163.                 fgWidth := 640;
  1164.                 fgHeight := 480;
  1165.                 if (band(ControlReg^, $40) <> 0) then begin
  1166.                         fgWidth := 768;
  1167.                         fgHeight := 512;
  1168.                         if BufferSize < PalBufferSize then
  1169.                             BufferSize := PalBufferSize;
  1170.                     end;
  1171.                 MaxLG3Frames := buffers;
  1172.                 SetupFGPort;
  1173.                 ResetScionLG3;
  1174.                 exit(LookForFrameGrabbers);
  1175.             end;
  1176.             
  1177.             {Scion VG-5 PCI}
  1178.             model := ScionVG5PCIid;
  1179.             base0 := 0;
  1180.             base1 := 0;
  1181.             buffers:= 0;
  1182.             found := false;
  1183.             found := LookForPCIFrameGrabbers(model, base0, base1, buffers);
  1184.             if (found) then begin
  1185.                     fgSlotBase := base0;
  1186.                     fgSuperSlotBase0 := base1;
  1187.                     fgSuperSlotBase1 := base1 + 524288;
  1188.                     FrameGrabber := ScionVG5f;
  1189.                     PCIFrameGrabber := true;
  1190.                     ControlReg := ptr(fgSlotBase + VG5ControlRegOffset);
  1191.                     ChannelReg := ptr(fgSlotBase + VG5ChannelRegOffset);
  1192.                     BufferReg := ptr(fgSlotBase + VG5BufferRegOffset);
  1193.                     DacHighReg := ptr(fgSlotBase + DacHighRegOffset);
  1194.                     DacLowReg := ptr(fgSlotBase + DacLowRegOffset);
  1195.                     fgWidth := 640;
  1196.                     fgHeight := 480;
  1197.                     if (band(ControlReg^, $40) <> 0) then begin
  1198.                             fgWidth := 768;
  1199.                             fgHeight := 512;
  1200.                             if BufferSize < PalBufferSize then
  1201.                                 BufferSize := PalBufferSize;
  1202.                         end;
  1203.                     SetupFGPort;
  1204.                     ResetScionVG5f;
  1205.                     exit(LookForFrameGrabbers);
  1206.                 end;
  1207.             
  1208.             {$endc} {if PowerPC}
  1209.  
  1210.     end; {LookForFrameGrabbers}
  1211.  
  1212.  
  1213.     procedure CheckBits;
  1214.         const
  1215.             QD32Trap = $AB03;
  1216.             UnimplementedTrap = $A89F;
  1217.         var
  1218.             MainDevice: GDHandle;
  1219.             myEnvRec: SysEnvRec;
  1220.             err: OSErr;
  1221.     begin
  1222.         err := SysEnvirons(1, myEnvRec);
  1223.         if err <> envNotPresent then
  1224.             with MyEnvRec do
  1225.                 begin
  1226.                     if not HasColorQD then
  1227.                         begin
  1228.                             PutError('Sorry, NIH Image requires a Macintosh with Color QuickDraw.');
  1229.                             ExitToShell;
  1230.                         end;
  1231.                     if not HasFPU and UsingFPU then
  1232.                         begin
  1233.                             PutError('Use the NonFPU version of NIH Image on Macs without a floating-point coprocessor.');
  1234.                             ExitToShell;
  1235.                         end;
  1236.                     OldSystem := systemVersion < $0605;
  1237.                     System7 := systemVersion >= $0700;
  1238.                     if not System7 then
  1239.                         begin
  1240.                             PutError('System 7 required.');
  1241.                             if not OptionKeyDown then
  1242.                                 ExitToShell;
  1243.                         end;
  1244.                     SystemRefnum := sysVRefNum;
  1245.                 end;
  1246.         Has32BitQuickDraw := nGetTrapAddress(QD32Trap, ToolTrap) <> nGetTrapAddress(UnimplementedTrap, ToolTrap);
  1247.         MainDevice := GetMainDevice;
  1248.         ScreenPixMap := MainDevice^^.gdPMap;
  1249.         ScreenRowBytes := BitAnd(ScreenPixMap^^.rowBytes, $1fff);
  1250.         ScreenBase := ScreenPixMap^^.baseAddr;
  1251.         FindMonitors(ScreenPixMap^^.PixelSize);
  1252.     end;
  1253.  
  1254.  
  1255.     procedure SetupMenus;
  1256.         var
  1257.             i: integer;
  1258.     begin
  1259.         AppleMenuH := GetMenu(AppleMenu);
  1260.         InsertMenu(AppleMenuH, 0);
  1261.         FileMenuH := GetMenu(FileMenu);
  1262.         InsertMenu(FileMenuH, 0);
  1263.         EditMenuH := GetMenu(EditMenu);
  1264.         InsertMenu(EditMenuH, 0);
  1265.         OptionsMenuH := GetMenu(OptionsMenu);
  1266.         InsertMenu(OptionsMenuH, 0);
  1267.         ProcessMenuH := GetMenu(ProcessMenu);
  1268.         InsertMenu(ProcessMenuH, 0);
  1269.         AnalyzemenuH := GetMenu(AnalyzeMenu);
  1270.         InsertMenu(AnalyzemenuH, 0);
  1271.         SpecialMenuH := GetMenu(SpecialMenu);
  1272.         InsertMenu(SpecialMenuH, 0);
  1273.         StacksMenuH := GetMenu(StacksMenu);
  1274.         InsertMenu(StacksMenuH, 0);
  1275.         WindowsMenuH := GetMenu(WindowsMenu);
  1276.         InsertMenu(WindowsMenuH, 0);
  1277.  
  1278.         FontMenuH := GetMenu(FontMenu);
  1279.         InsertMenu(FontMenuH, -1);
  1280.         SizeMenuH := GetMenu(SizeMenu);
  1281.         InsertMenu(SizeMenuH, -1);
  1282.         StyleMenuH := GetMenu(StyleMenu);
  1283.         InsertMenu(StyleMenuH, -1);
  1284.         BinaryMenuH := GetMenu(BinaryMenu);
  1285.         InsertMenu(BinaryMenuH, -1);
  1286.         ArithmeticMenuH := GetMenu(ArithmeticMenu);
  1287.         InsertMenu(ArithmeticMenuH, -1);
  1288.         fftMenuH := GetMenu(fftMenu);
  1289.         InsertMenu(fftMenuH, -1);
  1290.         BackgroundMenuH := GetMenu(BackgroundMenu);
  1291.         InsertMenu(BackgroundMenuH, -1);
  1292.         PropagateMenuH := GetMenu(PropagateMenu);
  1293.         InsertMenu(PropagateMenuH, -1);
  1294.         ColorTablesMenuH := GetMenu(ColorTablesMenu);
  1295.         InsertMenu(ColorTablesMenuH, -1);
  1296.         AcquireMenuH := GetMenu(AcquireMenu);
  1297.         InsertMenu(AcquireMenuH, -1);
  1298.         ExportMenuH := GetMenu(ExportMenu);
  1299.         InsertMenu(ExportMenuH, -1);
  1300.         FilterMenuH := GetMenu(FilterMenu);
  1301.         InsertMenu(FilterMenuH, -1);
  1302.  
  1303.         TransferModeMenuH := GetMenu(TransferModeMenu);
  1304.         InsertMenu(TransferModeMenuH, -1);
  1305.  
  1306.         LineToolMenuH := GetMenu(LineToolMenu);
  1307.         InsertMenu(LineToolMenuH, -1);
  1308.  
  1309.         ImageMathOpsMenuH := GetMenu(ImageMathOpsMenu);
  1310.         InsertMenu(ImageMathOpsMenuH, -1);
  1311.  
  1312.         ImageListMenuH := GetMenu(ImageListMenu);
  1313.         InsertMenu(ImageListMenuH, -1);
  1314.  
  1315.         UnitsMenuH := GetMenu(UnitsMenu);
  1316.         InsertMenu(UnitsMenuH, -1);
  1317.  
  1318.         DrawMenuBar;
  1319.         AppendResMenu(AppleMenuH, 'DRVR');
  1320.         AppendResMenu(FontMenuH, 'FONT');
  1321.         NumFontItems := CountMItems(FontMenuH);
  1322.     end;
  1323.  
  1324.  
  1325.     function GetFolderDirID (parentFolderVRefNum: Integer; parentFolderDirID: LongInt; folderName: Str63; var folderDirID: LongInt): OSErr;
  1326.   {Given a folder's name, vRefNum, and parent dirID, find its dirID }
  1327.         var
  1328.             myCInfoPBRec: CInfoPBRec;
  1329.             retCode: OSErr;
  1330.     begin
  1331.         myCInfoPBRec.ioCompletion := nil;
  1332.         myCInfoPBRec.ioNamePtr := @folderName;
  1333.         myCinfoPBRec.ioVRefNum := parentFolderVRefNum;
  1334.         myCInfoPBRec.ioFDirIndex := 0; { use name, vRefNum, dirID }
  1335.         myCInfoPBRec.ioDrDirID := parentFolderDirID; { will be changed }
  1336.         retCode := PBGetCatInfoSync(@myCInfoPBRec); { IM IV-155 }
  1337.         if retCode = noErr then
  1338.             folderDirID := myCInfoPBRec.ioDrDirID;
  1339.         GetFolderDirID := retCode;
  1340.     end;
  1341.  
  1342.  
  1343.     procedure MyAppendMenu (menu: MenuHandle; item: integer; str: str255);
  1344.     begin
  1345.         AppendMenu(menu, ' ');
  1346.         SetMenuItemText(menu, item, str);
  1347.     end;
  1348.  
  1349.  
  1350.     procedure FindPlugIns;
  1351.         var
  1352.             err: OSErr;
  1353.             wdRefNum, index, SystemVRefNum: integer;
  1354.             name: Str255;
  1355.             ftype: OSType;
  1356.             PB: CInfoPBRec;
  1357.             ProcID, SystemDirID: LongInt;
  1358.             spec: FSSpec;
  1359.             pb2: WDPBRec;
  1360.             rCount, iFileRef: integer;
  1361.     begin
  1362.         if not System7 then
  1363.             exit(FindPlugIns);
  1364.         err := GetFolderDirID(LaunchVRefNum, LaunchDirID, 'Plug-Ins', PlugInsDirID);
  1365.         if err = NoErr then
  1366.             PlugInsVRefNum := LaunchVRefNum
  1367.         else if GetWDInfo(SystemRefNum, SystemVRefNum, SystemDirID, ProcID) = NoErr then
  1368.             if GetFolderDirID(SystemVRefNum, SystemDirID, 'Plug-Ins', PlugInsDirID) = NoErr then
  1369.                 PlugInsVRefNum := SystemVRefNum
  1370.             else
  1371.                 exit(FindPlugIns);
  1372.         err := OpenWD(PlugInsVRefNum, PlugInsDirID, 0, wdRefNum);
  1373.         if err <> NoErr then
  1374.             exit(FindPlugIns);
  1375.         index := 0;
  1376.         while true do
  1377.             begin
  1378.                 index := index + 1;
  1379.                 with PB do
  1380.                     begin
  1381.                         ioCompletion := nil;
  1382.                         ioNamePtr := @name;
  1383.                         ioVRefNum := wdRefNum;
  1384.                         ioDirID:=PlugInsDirID;
  1385.                         ioFDirIndex := index;
  1386.                         err := PBGetCatInfoSync(@PB); {ppc-bug}
  1387.                         if err = fnfErr then
  1388.                             leave;
  1389.                     end;
  1390.                 spec.vrefnum := PluginsVRefNum;
  1391.                 spec.parID := PlugInsDirID;
  1392.                 spec.name := name;
  1393.                 iFileRef := FSpOpenResFile(spec, fsCurPerm);
  1394.                 if iFileRef <> -1 then
  1395.                     begin
  1396.                         UseResFile(iFileRef);
  1397.                         rCount := Count1Resources('8BAM');
  1398.                         if rCount > 0 then
  1399.                             begin
  1400.                                 nAcqPlugIns := nAcqPlugIns + 1;
  1401.                                 if nAcqPlugIns = 1 then
  1402.                                     SetMenuItemText(AcquireMenuH, 1, name)
  1403.                                 else
  1404.                                     MyAppendMenu(AcquireMenuH, nAcqPlugIns, name);
  1405.                             end;
  1406.                         rCount := Count1Resources('8BEM');
  1407.                         if rCount > 0 then
  1408.                             begin
  1409.                                 nExportPlugIns := nExportPlugIns + 1;
  1410.                                 if nExportPlugIns = 1 then
  1411.                                     SetMenuItemText(ExportMenuH, 1, name)
  1412.                                 else
  1413.                                     MyAppendMenu(ExportMenuH, nExportPlugIns, name);
  1414.                             end;
  1415.                         rCount := Count1Resources('8BFM');
  1416.                         if rCount > 0 then
  1417.                             begin
  1418.                                 nFilterPlugIns := nFilterPlugIns + 1;
  1419.                                 if nFilterPlugIns = 1 then
  1420.                                     SetMenuItemText(FilterMenuH, 1, name)
  1421.                                 else
  1422.                                     MyAppendMenu(FilterMenuH, nFilterPlugIns, name);
  1423.                             end;
  1424.                         CloseResFile(iFileRef);
  1425.                     end;
  1426.                 ShowAnimatedWatch;
  1427.             end; {while}
  1428.         err := CloseWD(wdRefNum);
  1429.     end;
  1430.  
  1431.  
  1432.     procedure MakeGDevice;
  1433.   {Sets up an offscreen 8-bit graphics environment for use when the screen is not 8-bits.}
  1434.         var
  1435.             DeviceRect: Rect;
  1436.             h: CTabHandle;
  1437.             SaveDevice: GDHandle;
  1438.  
  1439.         procedure Abort;
  1440.         begin
  1441.             PutError('Error creating GDevice.');
  1442.             ExitToShell;
  1443.         end;
  1444.  
  1445.     begin
  1446.         SaveDevice := GetGDevice;
  1447.         SetRect(DeviceRect, 0, 0, 64, 64);
  1448.         h := GetCTable(AppleDefaultClut);
  1449.         if h = nil then
  1450.             abort;
  1451.         osGDevice := NewGDevice(0, -1);
  1452.         if osGDevice = nil then
  1453.             abort;
  1454.         with osGDevice^^ do
  1455.             begin
  1456.                 gdId := 0;
  1457.                 gdType := clutType;
  1458.                 gdResPref := 3;                                            { Color matching resolution. }
  1459.                 gdSearchProc := nil;
  1460.                 gdCompProc := nil;
  1461.                 gdFlags := $C401;                                            { 1100 0100 0000 0001 }
  1462.                 gdRect := DeviceRect;
  1463.                 with gdPMap^^ do
  1464.                     begin
  1465.                         baseAddr := nil;
  1466.                         bounds := DeviceRect;
  1467.                         rowBytes := 64 + $8000;
  1468.                         pixelSize := 8;
  1469.                         cmpCount := 1;
  1470.                         cmpSize := 8;
  1471.                         DisposeCTable(pmTable);
  1472.                         pmTable := h;
  1473.                     end;
  1474.             end;
  1475.         SetGDevice(SaveDevice);
  1476.     end;
  1477.  
  1478.  
  1479.     procedure LoadColorTable(PrefsStart, PrefsEnd: integer; PrefsInvert: boolean);
  1480.     {Load the pseudocolor LUT specified in preferences file.}
  1481.     begin
  1482.         with Info^ do begin
  1483.             if    LutMode = Pseudocolor then begin
  1484.                     SwitchColorTables(GetColorTableItem(ColorTable), false);
  1485.                     ColorStart := PrefsStart;
  1486.                     ColorEnd := PrefsEnd;
  1487.                     if PrefsInvert then
  1488.                         InvertPalette;
  1489.                     UpdateLut;
  1490.                 end;
  1491.             end;
  1492.     end;
  1493.  
  1494.  
  1495.     procedure Init;
  1496.         var
  1497.             i: integer;
  1498.             p: SyspPtr;
  1499.             mbhp: ^integer;
  1500.             str: str255;
  1501.             err: OSErr;
  1502.     begin
  1503.         SetApplLimit(ptr(ord4(GetApplLimit) - StackSize));
  1504.         MaxApplZone;
  1505.         for i := 1 to 20 do
  1506.             MoreMasters;
  1507.         InitGraf(@qd.thePort);
  1508.         InitFonts;
  1509.         InitWindows;
  1510.         InitCursor;
  1511.         TEInit;
  1512.         InitDialogs(@SysResume);
  1513.         CheckBits;
  1514.         ScreenPort := GrafPtr(NewPtr(SizeOf(GrafPort)));
  1515.         OpenPort(ScreenPort);
  1516.         NoInfo := @NoInfoRec;
  1517.         Info := NoInfo;
  1518.         InfoWindow := nil;
  1519.         with BlackRGB do
  1520.             begin
  1521.                 red := 0;
  1522.                 blue := 0;
  1523.                 green := 0;
  1524.             end;
  1525.         with WhiteRGB do
  1526.             begin
  1527.                 red := -1;
  1528.                 blue := -1;
  1529.                 green := -1;
  1530.             end;
  1531.  
  1532.         with NoInfo^ do
  1533.             begin
  1534.                 iVersion:=version;
  1535.                 nlines := 0;
  1536.                 PixelsPerLine := 0;
  1537.                 ImageSize := 0;
  1538.                 PixMapSize := 0;
  1539.                 PicBaseAddr := nil;
  1540.                 PicBaseHandle := nil;
  1541.                 osPort := nil;
  1542.                 RoiShowing := false;
  1543.                 RoiType := NoRoi;
  1544.                 RoiRect := SrcRect;
  1545.                 roiRgn := NewRgn;
  1546.                 title := 'NoInfo';
  1547.                 Magnification := 1.0;
  1548.                 PictureType := NullPicture;
  1549.                 wptr := nil;
  1550.                 Changes := false;
  1551.                 BytesPerRow := 0;
  1552.                 SetRect(SrcRect, 0, 0, 0, 0);
  1553.                 PicRect := SrcRect;
  1554.                 wrect := SrcRect;
  1555.                 initwrect := wrect;
  1556.                 savewrect := wrect;
  1557.                 SaveSrcRect := SrcRect;
  1558.                 SaveMagnification := magnification;
  1559.                 savehloc := 0;
  1560.                 savevloc := 0;
  1561.                 ScaleToFitWindow := false;
  1562.                 nColors := 256;
  1563.                 ColorStart := 0;
  1564.                 ColorEnd := 255;
  1565.                 SaveColorStart := 0;
  1566.                 SaveColorEnd := 255;
  1567.                 FillColor1 := BlackRGB;
  1568.                 FillColor2 := BlackRGB;
  1569.                 SaveFill1 := BlackRGB;
  1570.                 SaveFill2 := BlackRGB;
  1571.                 ColorTable := CustomTable;
  1572.                 LUTMode := GrayScale;
  1573.                 InvertedColorTable := false;
  1574.                 xUnit := 'pixel';
  1575.                 xScale := 0.0;
  1576.                 yScale := 0.0;
  1577.                 zScale := 1.0;
  1578.                 PixelAspectRatio := 1.0;
  1579.                 SpatiallyCalibrated := false;
  1580.                 UnitOfMeasure := '';
  1581.                 PicNum := 1;
  1582.                 PidNum := 0;
  1583.                 HeaderOffset := -1;
  1584.                 ImageDataOffset := -1;
  1585.                 ColorMapOffset := -1;
  1586.                 for i := 0 to 255 do
  1587.                     ctable[i].value := 0;
  1588.                 Fit := Uncalibrated;
  1589.                 ZeroClip := false;
  1590.                 nCoefficients := 0;
  1591.                 for i := 1 to 6 do
  1592.                     Coefficient[i] := 0.0;
  1593.                 BinaryPic := false;
  1594.                 WindowState := NormalWindow;
  1595.                 Revertable := false;
  1596.                 LX1 := -1.0;
  1597.                 LY1 := -1.0;
  1598.                 LX2 := -1.0;
  1599.                 LY2 := -1.0;
  1600.                 LAngle := 0.0;
  1601.                 IdentityFunction := false;
  1602.                 StackInfo := nil;
  1603.                 Thresholding := false;
  1604.                 fileVersion := 0;
  1605.                 vref := 0;
  1606.                 DataType := EightBits;
  1607.                 AbsoluteMin := 0;
  1608.                 AbsoluteMax := 0;
  1609.                 CurrentMin := 0;
  1610.                 CurrentMax := 0;
  1611.                 LittleEndian := false;
  1612.                 InvertedImage := false;
  1613.                 DataH := nil;
  1614.             end;
  1615.             
  1616.         MakeGDevice;
  1617.         LookForFrameGrabbers;
  1618.  
  1619.     {Default settings that can be changed by GetSettings.}
  1620.         ForegroundIndex := BlackIndex;
  1621.         BackgroundIndex := WhiteIndex;
  1622.         BrushWidth := 14;
  1623.         BrushHeight := 14;
  1624.         SprayCanDiameter := 32;
  1625.         SprayCanRadius := 16;
  1626.         SprayCanRadius2 := 256;
  1627.         CurrentFontID := Helvetica;
  1628.         CurrentStyle := []; {plain}
  1629.         CurrentSize := 12;
  1630.         TextJust := teJustLeft;
  1631.         TextBack := NoBack;
  1632.         nExtraColors := 0;
  1633.         InitExtraColors;
  1634.         InvertVideo := false;
  1635.         measurements := [AreaM, MeanM];
  1636.         InvertPlots := false;
  1637.         AutoscalePlots := true;
  1638.         LinePlot := true;
  1639.         DrawPlotLabels := true;
  1640.         FixedSizePlot := false;
  1641.         ProfilePlotWidth := 300;
  1642.         ProfilePlotHeight := 150;
  1643.         if FrameGrabber=ScionAG5 then begin
  1644.             FramesToAverage := 32;
  1645.             VideoRateAveraging:=true
  1646.         end else begin
  1647.             FramesToAverage := 16;
  1648.             VideoRateAveraging := false;
  1649.         end;
  1650.         NewPicWidth := 552;
  1651.         NewPicHeight := 436;
  1652.         BufferSize := DefaultBufferSize;
  1653.         ThresholdToForeground := true;
  1654.         NonThresholdToBackground := true;
  1655.         VideoChannel := 0;
  1656.         WhatToImport := ImportTIFF;
  1657.         ImportCustomWidth := 512;
  1658.         ImportCustomHeight := 512;
  1659.         ImportCustomOffset := 0;
  1660.         WandAdjustAreas := false;
  1661.         BinaryIterations := 1;
  1662.         ScaleArithmetic := true;
  1663.         InvertPixelValues := false;
  1664.         InvertYCoordinates := true;
  1665.         FieldWidth := 9;
  1666.         precision := 2;
  1667.         MinParticleSize := 1;
  1668.         MaxParticleSize := 999999;
  1669.         IgnoreParticlesTouchingEdge := false;
  1670.         LabelParticles := true;
  1671.         OutlineParticles := false;
  1672.         OscillatingMovies := false;
  1673.         MaxMeasurements := 256;
  1674.          ImportCustomDepth := EightBits;
  1675.         ImportSwapBytes := false;
  1676.         ImportCalibrate := true;
  1677.         ImportAutoScale := true;
  1678.         ShowHeadings := false;
  1679.         ProfilePlotMin := 0;
  1680.         ProfilePlotMax := 255;
  1681.         ImportMin := 0.0;
  1682.         ImportMax := 255.0;
  1683.         HighlightSaturatedPixels := false;
  1684.         BallRadius := 50;
  1685.         FasterBackgroundSubtraction := true;
  1686.         ScaleConvolutions := true;
  1687.         BinaryCount := 4;
  1688.         BinaryThreshold := BinaryCount * 255;
  1689.         HalftoneFrequency := 53;
  1690.         HalftoneAngle := 45;
  1691.         HalftoneDotFunction := true;
  1692.         SyncMode := NormalSync;
  1693.         SwitchLUTOnSuspend := false;
  1694.         ImportInvert := false;
  1695.         TextCreator := 'Imag';
  1696.         MathSubGain := 0.5;
  1697.         MathSubOffset := 128.0;
  1698.         MathGain := MathSubGain;
  1699.         MathOffset := MathSubOffset;
  1700.         fgScale := 1;
  1701.         UseBuiltinDigitizer := true;
  1702.         DigitizerMode := digitizeGrayscale;
  1703.         DigitizerStandard:= defaultStd;
  1704.         LUTFriendlyMode := false;
  1705.         {end default settings}
  1706.         
  1707.         GrayMapReady := false;
  1708.         GetSettings; {from 'Image Prefs' file}
  1709.  
  1710.         ExplicitPalette := NewPalette(256, nil, pmExplicit, 0);
  1711.         CScreenPort := CGrafPtr(NewPtr(SizeOf(CGrafPort)));
  1712.         OpenCPort(CScreenPort);
  1713.         SetPalette(WindowPtr(CScreenPort), ExplicitPalette, false);
  1714.         finished := false;
  1715.         FlushEvents(EveryEvent, 0);
  1716.         mbhp := pointer(MBarHeight);
  1717.         MenuBarHeight := mbhp^;
  1718.         SetRect(trect, tleft, ttop, tleft + twidth, ttop + theight);
  1719.         ToolWindow := NewCWindow(nil, trect, 'Tools', true, NoGrowDocProc, nil, false, 0);
  1720.         SetPalette(ToolWindow, ExplicitPalette, false);
  1721.         WindowPeek(ToolWindow)^.WindowKind := ToolKind;
  1722.         BringToFront(ToolWindow);
  1723.         ScreenWidth := qd.ScreenBits.Bounds.right;
  1724.         ScreenHeight := qd.ScreenBits.Bounds.bottom;
  1725.         SliceStart := 100;
  1726.         SliceEnd := 150;
  1727.         with SliceColor do
  1728.             begin
  1729.                 red := -1;
  1730.                 green := 0;
  1731.                 blue := 0;
  1732.             end;
  1733.         DensitySlicing := false;
  1734.         with NoInfo^ do
  1735.             if LutMode = grayscale then
  1736.                 ResetGrayMap
  1737.             else
  1738.                 LoadColorTable(ColorStart, ColorEnd, InvertedColorTable); {Use Image Prefs LUT}
  1739.         InitMenus;  {LUT must be setup before InitMenus}
  1740.         SetRect(gmrect, gmleft, gmtop, gmleft + gmwidth, gmtop + gmheight);
  1741.         SetRect(gmSlide1, gmrectleft, gmrectbottom + gmSlide1Offset, gmrectleft + gmSlideWidth, gmrectbottom + gmSlide1Offset + gmSlideHeight);
  1742.         gmSlide1i := gmSlide1;
  1743.         InsetRect(gmSlide1i, 1, 1);
  1744.         SetRect(gmSlide2, gmrectleft, gmrectbottom + gmSlide2Offset, gmrectleft + gmSlideWidth, gmrectbottom + gmSlide2Offset + gmSlideHeight);
  1745.         gmSlide2i := gmSlide2;
  1746.         InsetRect(gmSlide2i, 1, 1);
  1747.         SetRect(gmIcon1, gmIcon1left, gmrectbottom + gmIconOffset, gmIcon1left + gmIconWidth, gmrectbottom + gmIconOffset + gmIconHeight);
  1748.         SetRect(gmIcon2, gmIcon2left, gmrectbottom + gmIconOffset, gmIcon2left + gmIconWidth, gmrectbottom + gmIconOffset + gmIconHeight);
  1749.         MapWindow := NewWindow(nil, gmrect, 'Map', true, NoGrowDocProc, nil, false, 0);
  1750.         WindowPeek(MapWindow)^.WindowKind := MapKind;
  1751.         SetRect(MapRect1, gmRectLeft, gmRectTop, gmRectRight, gmRectBottom);
  1752.         MapRect2 := MapRect1;
  1753.         InsetRect(MapRect2, -2, -2);
  1754.         cheight := 256 + (2 + nExtraColors) * ExtraColorsHeight;
  1755.         SetRect(crect, cleft, ctop, cleft + cwidth, ctop + cheight);
  1756.         LUTWindow := NewCWindow(nil, crect, 'LUT', true, NoGrowDocProc, nil, false, 0);
  1757.         SetPalette(LUTWindow, ExplicitPalette, false);
  1758.         WindowPeek(LUTWindow)^.WindowKind := LUTKind;
  1759.         rleft := 2;
  1760.         if ScreenHeight > 480 then
  1761.             rtop := ScreenHeight - rheight - 5
  1762.         else
  1763.             rtop := 358;
  1764.         SetRect(trect, rleft, rtop, rleft + rwidth, rtop + rheight);
  1765.         InfoWindow := NewWindow(nil, trect, 'Info', true, NoGrowDocProc, nil, false, 0);
  1766.         BringToFront(InfoWindow);
  1767.         WindowPeek(InfoWindow)^.WindowKind := InfoKind;
  1768.         BuggyWatch := GetCursor(WatchCursor);
  1769.         InitTools;
  1770.         MakeCursors;
  1771.         nPics := 0;
  1772.         nextPid := -1;
  1773.         HistoWindow := nil;
  1774.         PlotWindow := nil;
  1775.         IsInsertionPoint := false;
  1776.         MaskRgn := NewRgn;
  1777.         PlotPICT := nil;
  1778.         CurrentUndoSize := 0;
  1779.         p := GetSyspPtr;
  1780.         BlinkTime := BitAnd($000F, p^.volclik) * 4;
  1781.         MakePatterns;
  1782.         PicLeftBase := twidth + cwidth + 10;
  1783.         PicTopBase := MenuBarHeight + 20;
  1784.         PicLeft := PicLeftBase;
  1785.         PicTop := PicTopBase;
  1786.         MouseState := NotInRoi;
  1787.         WhatsOnClip := NothingOnClip;
  1788.         OldScrapCount := GetScrapCount;
  1789.         ClipboardConverted := false;
  1790.         Digitizing := false;
  1791.         debugging := false;
  1792.         BlankFieldInfo := nil;
  1793.         PlotCount := 0;
  1794.         PlotAvg := 1;
  1795.         ActualPlotMin := 0.0;
  1796.         ActualPlotMax := 0.0;
  1797.         ClipTextInBuffer := false;
  1798.         TextBufP := TextBufPtr(NewPtr(SizeOf(TextBufType)));
  1799.         if TextBufP = nil then
  1800.             ExitToShell;
  1801.         MacrosP := MacrosPtr(NewPtr(SizeOf(MacrosRec)));
  1802.         if MacrosP = nil then
  1803.             ExitToShell;
  1804.         mCount := 0;
  1805.         mCount2 := 0;
  1806.         nPoints := 0;
  1807.         nLengths := 0;
  1808.         nAngles := 0;
  1809.         for i := 1 to MaxStandards do
  1810.             StandardValues[i] := BadReal;
  1811.         nStandards := 0;
  1812.         nKnownValues := 0;
  1813.         for i := 0 to 255 do
  1814.             cvalue[i] := i;
  1815.         WhatToUndo := NothingToUndo;
  1816.         WhatToCopy := NothingToCopy;
  1817.         PrintRecord := nil;
  1818.         printing := false;
  1819.         cr := chr(13);
  1820.         crStr:=chr(13);
  1821.         tab := chr(9);
  1822.         BackSpace := chr(8);
  1823.         eofChr := chr(4);
  1824.         measuring := false;
  1825.         UpdateFitEllipse;
  1826.         nListColumns := 0;
  1827.         if FrameGrabber=ScionAG5 then
  1828.             VideoRateAveraging:=true;
  1829.         RestoreUndoBuf := true;
  1830.         SelectionMode := NewSelection;
  1831.         RoiMovementState := Unconstrained;
  1832.         PasteControl := nil;
  1833.         PasteTransferMode := SrcCopy;
  1834.         ImportCustomSlices := 1;
  1835.         ImportAll := false;
  1836.         RoiNudged := false;
  1837.         OpPending := false;
  1838.         RedoSelection := false;
  1839.         WandAutoMeasure := false;
  1840.         OptionKeyWasDown := false;
  1841.         InfoMessage := '';
  1842.         SaveAsWhat := asTIFF;
  1843.         ExportAsWhat := asRaw;
  1844.         AnalyzingParticles := false;
  1845.         RedirectSampling := false;
  1846.         IncludeHoles := false;
  1847.         SaveAllState := NoSaveAll;
  1848.         ContinuousHistogram := false;
  1849.         DrawLabels(' ', '', '');
  1850.         HistogramSliceStart := 0;
  1851.         HistogramSliceEnd := 255;
  1852.         RoiUpdateTime := 0;
  1853.         MeasurementToRedo := 0;
  1854.         nMacros := 0;
  1855.         macro := false;
  1856.         KernelsRefNum := 0;
  1857.         DefaultRefNum := 0;
  1858.         MacrosRefNum := 0;
  1859.         LaunchVRefNum := 0;
  1860.         LaunchDirID := 0;
  1861.         PlugInsVRefNum := 0;
  1862.         PlugInsDirID := 0;
  1863.         DefaultFileName := '';
  1864.         rsHScale := 1.0;
  1865.         rsVScale := 1.0;
  1866.         rsAngle := 0.0;
  1867.         rsMethod := NearestNeighbor;
  1868.         rsCreateNewWindow := true;
  1869.         rsInteractive := false;
  1870.         DriverHalftoning := true;
  1871.         LivePasteMode := false;
  1872.         ShowCount := true;
  1873.         PasteControlLeft := ScreenWidth - pcwidth - 10;
  1874.         PasteControlTop := trunc(ScreenHeight * 0.2);
  1875.         MaxCoordinates := 10000;
  1876.         ResultsLeft := PicLeftBase;
  1877.         ResultsTop := 56;
  1878.         UnsavedResults := false;
  1879.         MajorLabel := 'Major';
  1880.         MinorLabel := 'Minor';
  1881.         User1Label := 'User 1';
  1882.         User2Label := 'User 2';
  1883.         MakingLOI := false;
  1884.         LOIType := Straight;
  1885.         MakingStack := false;
  1886.         FramesWanted := 20;
  1887.         DelayTicks := 0;
  1888.         CmdPeriodToStop := 'Type cmd-''.'' to stop.';
  1889.         CommandKey := chr(17);
  1890.         ClipboardColor := BlackRGB;
  1891.         InitAngle := 0;                                             {default is one full revolution of volume}
  1892.         TotalAngle := 360;
  1893.         AngleInc := 10;                                           {in ten-degree increments}
  1894.         TransparencyUpper := 254;                      {default has only value 255 transparent}
  1895.         TransparencyLower := 0;
  1896.         DepthCueSurf := 0;                                     {default has full depth-cueing for surface}
  1897.         DepthCueInt := 50;                                     {components and 50% depth-cueing elsewhere}
  1898.         Opacity := 0;
  1899.         SaveProjections := FALSE;                         {by default, do not save and close projections}
  1900.         AxisOfRotation := xAxis;
  1901.         MinProjSize := TRUE;                                {by default, make all windows just big enough}
  1902.         ProjectionMethod := NearestPoint;
  1903.         MeterWindow := nil;
  1904.         nCoordinates := 0;
  1905.         CoordinatesWidth := 0;
  1906.         CoordinatesHeight := 0;
  1907.         CoordinatesRoiType := NoRoi;
  1908.         SumFrames := false;
  1909.         ShowIntegratedValues := false;
  1910.         FixIntegrationScale := false;
  1911.         IntegrationMin := 0;
  1912.         IntegrationMax := 4096;
  1913.         ExternalTrigger := false;
  1914.         BlindMovieCapture := false;
  1915.         VideoControl := nil;
  1916.         DitherColor := true;
  1917.         RGBLut := CustomLUT;
  1918.         with Highlight1 do
  1919.             begin
  1920.                 red := -1;
  1921.                 green := 0;
  1922.                 blue := 0;
  1923.             end;
  1924.         with Highlight254 do
  1925.             begin
  1926.                 red := -1;
  1927.                 green := -1;
  1928.                 blue := 0;
  1929.             end;
  1930.         HighlightMode := false;
  1931.         LegalWithoutImage := [UpdateLutC, StartC, ShowResultsC, GetPicSizeC, PutMsgC, ExitC, CaptureC, MoveToC, BeepC, MakeNewC, SetPaletteC, SetBackC, GetRoiC, OpenC, ImportC, SetImportC, SetMinMaxC, SetCustomC, nPicsC, WaitC, SetSizeC, TriggerC, AverageFramesC, SaveStateC, RestoreStateC, SetCounterC, MakeStackC, ShowMsgC, SetUser1C, SetUser2C, SetOptionsC, RequiresC];
  1932.         LegalWithoutImage := LegalWithoutImage + [DisposeAllC, GetTimeC, SetForeC, SetBackC, GetStringC, OpenSerialC, GetSerialC, PutSerialC, SetCursorC, TickCountC, ConcatC, SetVideoC, StringToNumC, ChannelC, AcquireC, UndoSizeC, CallFilterC, PhotoModeC, DrawTextC, WriteC, WritelnC, NewTextWindowC, UserCodeC, SelectWindowC, BitAndC, BitOrC, WindowTitleC, ScaleConvolutionsC];
  1933.         LegalWithoutImage := LegalWithoutImage + [SaveC, SaveAsC, CloseC, DisposeC, PosC, DeleteC, LengthC, SetPrecisionC, SetExportC, ExportC, SetFontC, SetFontSizeC, SetTextC, GetC, MoveWindowC, GetFileInfoC, SelectToolC];
  1934.         TopOfStack := 0;
  1935.         SerialBufferP := nil;
  1936.         OpeningPlugInWindow := false;
  1937.         LG3DacA := 0;
  1938.         LG3DacB := 0;
  1939.         LG3DataOut := 0;
  1940.         nAcqPlugIns := 0;
  1941.         nExportPlugIns := 0;
  1942.         nFilterPlugIns := 0;
  1943.         LastAcqPlugIn := '';
  1944.         LastFilterPlugIn := '';
  1945.         LastExportPlugIn := '';
  1946.         TextInfo := nil;
  1947.         nTextWindows := 0;
  1948.         NewTitle := 'Untitled';
  1949.         CurrentWPtr := nil;
  1950.         CurrentKind := 0;
  1951.         SearchString := '';
  1952.         CurrentMathOp := SubMath;
  1953.         MathSrc1 := 1;
  1954.         MathSrc2 := 1;
  1955.         MathResult := 'Result';
  1956.         APReset := true;
  1957.         ShowPlot := true;
  1958.         FiducialMethod := OnScreen;
  1959.         ConfirmFidClicks := false;
  1960.         WatchIndex := 1;
  1961.         WireframeSurfacePlots := true;
  1962.         GrayscaleSurfacePlots := false;
  1963.         RankFilter := MedianRank;
  1964.         RankIterations := 1;
  1965.         TextScrollActionProc:=nil;
  1966.         AboutBoxFilterProc:=nil;    
  1967.         AboutBoxUserProc:=nil;
  1968.         ResultsScrollActionProc:=nil;
  1969.         ImageMathUserProc:=nil;
  1970.         SetScaleUserProc:=nil;
  1971.         OpenDHookProc:=nil;
  1972.         SaveAsDHookProc:=nil;
  1973.         ImportDHookProc:=nil;
  1974.         ExportDHookProc:=nil;
  1975.         BitInfoProc:=nil;
  1976.         GetPictDataProc:=nil;
  1977.         TestAbortProc:=nil;
  1978.         UpdateProgressProc:=nil;
  1979.         AG5LutMode := false;
  1980.         AG5GrabMode := GrabNormal;
  1981.         AG5BufferMode := Read0Lut1Grab0;
  1982.         IntegrateOnChip:=false;
  1983.         OpeningFinderFiles:=false;
  1984.         gNumberSlices:=true;
  1985.         gBorders:=true;
  1986.         RealImageMath:=false;
  1987.         DicomInitialized := false;
  1988.         ImportingDicom := false;
  1989.         err := HGetVol(nil, StartupSpec.vRefNum, StartupSpec.parID);
  1990.         OpeningRGB := false;
  1991.         PrecisionTiming := false;
  1992.         LG3BufferCapture := false;
  1993.         TimeStamp := false;
  1994.         SecondsPerFrame := 0.0;
  1995.         TriggerFirstFrameOnly := true;
  1996.         UseExistingStack := false;
  1997.         RealArithmetic := false;
  1998.         vdig := nil;
  1999.         GWorldLUT := nil;
  2000.         osGWorld := nil;
  2001.         if InvertPixelValues then
  2002.             InvertGrayLevels;
  2003. end;
  2004.  
  2005.  
  2006. end.